﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Goods
{
    public partial class List : ListPage
    {
        BLL.V_Goods bllList = new BLL.V_Goods();
        BLL.Goods bll = new BLL.Goods();
        //
        BLL.Pic bllPic = new BLL.Pic();
        BLL.Goods_BuyPrice_Log bllBuyPrice_Log = new BLL.Goods_BuyPrice_Log();
        BLL.Goods_SellPrice_Log bllSellPrice_Log = new BLL.Goods_SellPrice_Log();
        protected void Page_Load(object sender, EventArgs e)
        {
            Master.show_listControls = true;
            if (!IsPostBack)
            {
                search_select_category.SelectedValue = DataTypePlus.requestString(Request.QueryString["search_select_category_tb_Category"]);
            }
            if (Security.GetCurrentUserType() == EnumHelper.UserType.Supplier) Master.BasicSQL = "SupplierID=" + Security.GetCurrentSupplierID();
            if (Security.InUserType(new List<EnumHelper.UserType>() { EnumHelper.UserType.Buyer, EnumHelper.UserType.Supplier }))
            {//采购员+供货商 看不到：售价
                th_SellPrice.Visible = false;
                Master.ArrayControlID_noVisible_cph_td.Add("td_SellPrice");
                if (Security.GetCurrentUserType() == EnumHelper.UserType.Supplier)
                {//供应商 看不到：供货商名称
                    th_Supplier.Visible = false;
                    Master.ArrayControlID_noVisible_cph_td.Add("td_SupplierName");
                    group_search_supplier.Visible = false;
                }
            }
            if (Security.GetCurrentUserType() == EnumHelper.UserType.Store)
            {//孕婴店  看不到：进价
                th_BuyPrice.Visible = false;
                Master.ArrayControlID_noVisible_cph_td.Add("td_BuyPrice");
            }
        }
        public override void buildSQL_Page_List_search()
        {
            Master.SearchSQL = "";
            if (search_text_fuzzy.Text != "")
            {
                string fuzzyText=search_text_fuzzy.Text.Replace("　","|").Replace(" ","|").Replace("，","|").Replace(",","|").Replace("||","|");
                string fuzzySQL = "";
                foreach (string fuzzyVal in fuzzyText.Split('|'))
                {
                    if (fuzzySQL != "") fuzzySQL += " or ";
                    fuzzySQL += " name like '%" + fuzzyVal + "%' or  number like '%" + fuzzyVal + "%' or  Code like '%" + fuzzyVal + "%' or brandname like '%" + fuzzyVal + "%'";
                }
                Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, " ( " + fuzzySQL + ")");
            }
            if (search_select_brand.SelectedValue != "" && search_select_brand.SelectedValue != "0")
                Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, "BrandID=" + search_select_brand.SelectedValue + "");
            if (search_select_category.SelectedValue != "" && search_select_category.SelectedValue != "0")
            {
                Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, "'|'+CategoryID_Tree like '%|" + search_select_category.SelectedValue + "|%'");
                search_select_category.SelectedValue = search_select_category.SelectedValue;
            }
            if (search_select_supplier.SelectedValue != "" && search_select_supplier.SelectedValue != "0")
                Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, "SupplierID=" + search_select_supplier.SelectedValue + "");
            if(search_radio_state.SelectedValue!="")
                Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, "State=" + search_radio_state.SelectedValue + "");
            if (search_select_comefrom.SelectedValue != "" && search_select_comefrom.SelectedValue != "0")
                Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, "ComeFrom=" + search_select_comefrom.SelectedValue + "");
        }
        public override DataSet get_Page_DataSet(string sql_, string orderby_, int pageFrom_, int pageTo_) { return bllList.GetListByPage(sql_, orderby_, pageFrom_, pageTo_); }
        public override void del_Page_List(string inStr_)
        {
            foreach (string id in inStr_.Split(','))
            {
                int thisID = DataTypePlus.requestInt(id);
                foreach (Model.Pic pic in bllPic.GetModelList("GoodsID=" + thisID))
                {
                    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Pic, pic.id, EnumHelper.OperateType.Delete, "删除商品");
                    bllPic.Delete(pic.id);
                }
                foreach (Model.Goods_BuyPrice_Log log in bllBuyPrice_Log.GetModelList("GoodsID=" + thisID))
                {
                    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Goods_BuyPrice_Log, log.id, EnumHelper.OperateType.Delete, "删除商品");
                    bllBuyPrice_Log.Delete(log.id);
                }
                foreach (Model.Goods_SellPrice_Log log in bllSellPrice_Log.GetModelList("GoodsID=" + thisID))
                {
                    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Goods_SellPrice_Log, log.id, EnumHelper.OperateType.Delete, "删除商品");
                    bllSellPrice_Log.Delete(log.id);
                }

                bll.Delete(thisID);
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Goods, thisID, EnumHelper.OperateType.Delete, "");
            }
        }

    }
}